package com.kaisa.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaisa.activity.R;
import com.kaisa.activity.bean.AllGoBean;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Administrator on 2015/12/13.
 */
public class AllGoAdapter extends AppBaseAdapter<AllGoBean.DataEntity>{

    public AllGoAdapter(List<AllGoBean.DataEntity> list, Context context) {
        super(list, context);
        inflater = LayoutInflater.from(context);
    }


    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_list_allgo,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //映射数据
        StringBuffer starttime = new StringBuffer();
        for (int i = 0; i < list.get(position).getStart_date().size(); i++) {
            starttime.append(list.get(position).getStart_date().get(i)+",");
        }
        String url = "http://imgs.caissa.com.cn/pic/" + list.get(position).getImage();
        try {
            x.image().bind(holder.img, URLEncoder.encode(url,"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.title.setText(list.get(position).getTitle());
        holder.start.setText(starttime.toString());
        holder.tag.setText(list.get(position).getTags().get(0));
        holder.tags.setText(list.get(position).getTags().get(1));
        holder.price.setText(list.get(position).getPrice());
        return convertView;
    }

    public static class ViewHolder extends BaseViewHolder{
        @ViewInject(R.id.iv_allgo_img)
        private ImageView img;
        @ViewInject(R.id.tv_allgo_title)
        private TextView title;
        @ViewInject(R.id.tv_allgo_start)
        private TextView start;
        @ViewInject(R.id.tv_allgo_tag)
        private TextView tag;
        @ViewInject(R.id.tv_allgo_tags)
        private TextView tags;
        @ViewInject(R.id.tv_allgo_price)
        private TextView price;



        public ViewHolder(View convertView) {
            super(convertView);
            x.view().inject(this,convertView);
        }
    }
}
